using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class YH_ScreeningPlanProjectService : BaseAppService<YH_ScreeningPlanProjectDto, YH_ScreeningPlanProject, YH_ScreeningPlanProjectRepository>
    {
        readonly ExcelHelp _excelHelp;
        public YH_ScreeningPlanProjectService(YH_ScreeningPlanProjectRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<YH_ScreeningPlanProject, YH_ScreeningPlanProjectDto> GetLoadPageOffsetMode(PagingModel<YH_ScreeningPlanProjectDto> pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking().Include(a => a.SysArea).Include(a => a.YH_ScreeningPlan);
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

             .Add("ScreeningPlanId", "=") //隐患排查计划主表ID
             .Add("Project", "=") //排查项目
             .Add("Region", "=") //涉及区域ID
             .Add("Remark", "=");//备注说明


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            //loadPageOffsetModel.Selector = model => new YH_ScreeningPlanProject()
            //{
            //    Id = model.Id,
            //    ScreeningPlanId = model.ScreeningPlanId,//隐患排查计划主表ID
            //    Project = model.Project,//排查项目
            //    Region = model.Region,//涉及区域ID
            //    Remark = model.Remark,//备注说明
            //};
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<YH_ScreeningPlanProjectDto>> GetList(PagingModel<YH_ScreeningPlanProjectDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string> Export(PagingModel<YH_ScreeningPlanProjectDto> pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.ScreeningPlanId,
                ScreeningPlanId_title = "隐患排查计划主表ID",
                model.Project,
                Project_title = "排查项目",
                model.Region,
                Region_title = "涉及区域ID",
                model.Remark,
                Remark_title = "备注说明",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
